﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class ConnFireHydrant
	{
		public string ConnMSG { get; set; }

		public double BackConnPipeLength
		{
			get
			{
				return this.backConnPipeLength;
			}
			set
			{
				this.backConnPipeLength = value;
			}
		}

		public ConnFireHydrant(FamilyInstance familyInstance, IList<Pipe> pipes)
		{
			this.fiHyDrant = familyInstance;
			this.listPipe = pipes;
			this.mainPipe = this.listPipe.First<Pipe>();
			this.hyPipe = new YJKPiping<Pipe>(this.mainPipe);
			this.doc = this.mainPipe.Document;
			this.BackConnPipeLength = 0.65616797900262469;
			if (familyInstance.Symbol.FamilyName == "带灭火器箱组合式消防柜_双栓")
			{
				this.beDouble = true;
			}
		}

		public bool ConnectHydrant(string connType)
		{
			this.ConnMSG = "";
			bool flag = this.CheckSegmentFitting();
			if (!flag)
			{
				return flag;
			}
			this.fiHyDrant = this.MirroHydrant(this.fiHyDrant, this.listPipe);
			if (!(connType == "1"))
			{
				if (!(connType == "2"))
				{
					if (!(connType == "3"))
					{
						if (connType == "4")
						{
							flag = this.BackConnHydrantConning(this.hyPipe, this.fiHyDrant);
						}
					}
					else
					{
						flag = this.SideConnGroupHydrantConning(this.hyPipe, this.fiHyDrant);
					}
				}
				else
				{
					flag = this.DownConnHydrantConning(this.hyPipe, this.fiHyDrant);
				}
			}
			else
			{
				flag = this.SideConnHydrantConning(this.hyPipe, this.fiHyDrant);
			}
			if (!flag && string.IsNullOrEmpty(this.ConnMSG))
			{
				this.ConnMSG = "管道位置不合适，无法进行连接！";
			}
			return flag;
		}

		private bool BeGroupHydrant(FamilyInstance hydrant)
		{
			bool result = false;
			if (hydrant.Symbol.FamilyName == "带灭火器箱组合式消防柜_双栓" || hydrant.Symbol.FamilyName == "带灭火器箱组合式消防柜_单栓")
			{
				result = true;
			}
			return result;
		}

		private bool SideConnHydrantConning(YJKPiping<Pipe> hyPipe, FamilyInstance drant)
		{
			bool result = false;
			if (this.BeGroupHydrant(drant))
			{
				this.ConnMSG = "请选择普通消火栓进行连接！";
				return result;
			}
			Connector connector = null;
			Connector connector2 = null;
			Connector connector3 = null;
			this.GetSideAndDownOrBackConns(drant, ref connector, ref connector2, ref connector3);
			if (connector == null)
			{
				this.ConnMSG = "未找到消火栓侧接口！";
				return result;
			}
			if (connector.IsConnected)
			{
				this.ConnMSG = "消火栓侧接口已连接！";
				return result;
			}
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			XYZ basisZ = connector.CoordinateSystem.BasisZ;
			if (!hyline.LineVector.IsParallelTo(basisZ) && !hyline.LineVector.IsPerpendicularTo(basisZ))
			{
				this.ConnMSG = "连接管道与接口引出管不平行且不垂直，不能进行连接！";
				return result;
			}
			if (YJKRevitTools.isRiserPipe(this.mainPipe))
			{
				result = this.RiserPipeConnSideDrant(connector, hyPipe);
			}
			else
			{
				result = this.HorizontalPipeConnSideDrant(connector, hyPipe);
			}
			return result;
		}

		private bool DownConnHydrantConning(YJKPiping<Pipe> hyPipe, FamilyInstance drant)
		{
			bool result = false;
			if (this.BeGroupHydrant(drant))
			{
				this.ConnMSG = "请选择普通消火栓进行连接！";
				return result;
			}
			Connector connector = null;
			Connector connector2 = null;
			Connector connector3 = null;
			this.GetSideAndDownOrBackConns(drant, ref connector, ref connector2, ref connector3);
			if (connector2 == null)
			{
				this.ConnMSG = "未找到消火栓底接口！";
				return result;
			}
			if (connector2.IsConnected)
			{
				this.ConnMSG = "消火栓底接口已连接！";
				return result;
			}
			if (connector == null)
			{
				this.ConnMSG = "消火栓侧接口不能为空！";
				return result;
			}
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			XYZ basisZ = connector.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(hyline.LineVector, basisZ))
			{
				this.ConnMSG = "连接管道与接口引出管不平行且不垂直，不能进行连接！";
				return result;
			}
			double paraDouble = this.GetParaDouble(drant);
			double pipeLength = paraDouble + this.ExtendPipeLength;
			if (YJKRevitTools.isRiserPipe(hyPipe.Piping))
			{
				pipeLength = paraDouble + 0.1;
				Pipe extractionPipe = this.ExtractionPipe(connector2, connector, pipeLength);
				result = this.ConnExtractionPipeAndRiserPipe(hyPipe.Piping, extractionPipe, true);
			}
			else
			{
				pipeLength = paraDouble + this.ExtendPipeLength;
				Pipe extractionPipe = this.ExtractionPipe(connector2, connector, pipeLength);
				result = this.ConnExtractionPipeAndMainPipe(hyPipe.Piping, extractionPipe);
			}
			return result;
		}

		private double GetParaDouble(FamilyInstance drant)
		{
			double result = 0.0;
			Parameter parameter = drant.GetParameter("下进水口距侧面距离");
			if (parameter == null)
			{
				IEnumerator enumerator = drant.Symbol.Parameters.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						Parameter parameter2 = (Parameter)obj;
						if (parameter2.Definition.Name == "下进水口距侧面距离")
						{
							result = Convert.ToDouble(parameter2.AsValueString()) / 304.8;
							break;
						}
					}
					return result;
				}
			}
			result = parameter.AsDouble();
			return result;
		}

		private bool BackConnHydrantConning(YJKPiping<Pipe> hyPipe, FamilyInstance drant)
		{
			bool result = false;
			if (!this.BeGroupHydrant(drant))
			{
				this.ConnMSG = "请选择组合式消火栓进行连接！";
				return result;
			}
			List<Connector> list = null;
			List<Connector> list2 = null;
			this.GetSideAndDownOrBackConns(drant, ref list, ref list2);
			if (list2 == null || list2.Count < 1)
			{
				this.ConnMSG = "未找到消火栓后连接口！";
				return result;
			}
			Connector connToConnector = this.GetConnToConnector(list2, hyPipe);
			if (connToConnector == null)
			{
				this.ConnMSG = "未找到消火栓后连接口！";
				return result;
			}
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			XYZ basisZ = connToConnector.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(hyline.LineVector, basisZ))
			{
				this.ConnMSG = "连接管道与接口引出管不平行且不垂直，不能进行连接！";
				return result;
			}
			if (YJKRevitTools.isRiserPipe(hyPipe.Piping))
			{
				Pipe extractionPipe = this.ExtractionPipe(connToConnector, 0.071084864391951011);
				result = this.ConnExtractionPipeAndRiserPipe(hyPipe.Piping, extractionPipe, false);
			}
			else
			{
				Pipe extractionPipe = this.ExtractionPipe(connToConnector, this.ExtendPipeLength);
				result = this.ConnExtractionPipeAndMainPipe(hyPipe.Piping, extractionPipe);
			}
			return result;
		}

		private bool SideConnGroupHydrantConning(YJKPiping<Pipe> hyPipe, FamilyInstance drant)
		{
			bool result = false;
			if (!this.BeGroupHydrant(drant))
			{
				this.ConnMSG = "请选择组合式消火栓进行连接！";
				return result;
			}
			List<Connector> listConn = null;
			List<Connector> list = null;
			this.GetSideAndDownOrBackConns(drant, ref listConn, ref list);
			Connector connToConnector = this.GetConnToConnector(listConn, hyPipe);
			if (connToConnector == null)
			{
				return result;
			}
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			XYZ basisZ = connToConnector.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(hyline.LineVector, basisZ))
			{
				this.ConnMSG = "连接管道与接口引出管不平行且不垂直，不能进行连接！";
				return result;
			}
			if (YJKRevitTools.isRiserPipe(this.mainPipe))
			{
				result = this.RiserPipeConnSideDrant(connToConnector, hyPipe);
			}
			else
			{
				result = this.HorizontalPipeConnSideDrant(connToConnector, hyPipe);
			}
			return result;
		}

		private Connector GetConnToConnector(List<Connector> listConn, YJKPiping<Pipe> hyPipe)
		{
			Connector result = null;
			if (listConn == null || listConn.Count < 1)
			{
				this.ConnMSG = "未找到消火栓接口！";
			}
			else if (listConn.Count == 1)
			{
				if (listConn.First<Connector>().IsConnected)
				{
					this.ConnMSG = "消火栓接口已连接！";
				}
				else
				{
					result = listConn.First<Connector>();
				}
			}
			else if (listConn.Count == 2)
			{
				Connector connector = listConn.First<Connector>();
				Connector connector2 = listConn.Last<Connector>();
				if (connector.IsConnected && !connector2.IsConnected)
				{
					result = connector2;
				}
				else if (connector2.IsConnected && !connector.IsConnected)
				{
					result = connector;
				}
				else if (connector.IsConnected && connector2.IsConnected)
				{
					this.ConnMSG = "消火栓接口均已连接！";
				}
				else
				{
					double num = hyPipe.GetProjectPoint(connector.Origin, true).DistanceTo(connector.Origin);
					double num2 = hyPipe.GetProjectPoint(connector2.Origin, true).DistanceTo(connector2.Origin);
					result = ((num < num2) ? connector : connector2);
				}
			}
			return result;
		}

		private Connector GetPipeUnConnector(YJKPiping<Pipe> hyPipe)
		{
			if (!hyPipe.StartConnector.Connector.IsConnected)
			{
				return hyPipe.StartConnector.Connector;
			}
			return hyPipe.EndConnector.Connector;
		}

		private double GetCheckDeviation()
		{
			return this.hyPipe.GetDiameter() * 1.5 + 0.10662729658792651;
		}

		private bool RiserPipeConnSideDrant(YJKPiping<Pipe> hyPipe, Connector sideConn)
		{
			bool result = false;
			XYZ xyz = sideConn.CoordinateSystem.BasisZ.Normalize();
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(sideConn.Origin);
			if (xyz.IsAlmostEqualTo((sideConn.Origin - project).Normalize()))
			{
				this.ConnMSG = "选择的立管位置不合适，无法与消火栓侧边连接。";
				return result;
			}
			XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(sideConn, 0.21325459317585302);
			XYZ xyz2 = (project - connectorDirectionPoint).Normalize();
			if (xyz.AngleTo(xyz2) / Math.PI * 180.0 < 90.0)
			{
				Pipe pipe = YJKRevitTools.NewPipe(hyPipe.Piping, connectorDirectionPoint, sideConn.Origin);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
				this.PipeConnectHydrant(sideConn, pipe);
				this.doc.Regenerate();
				return !(this.ConnMSG != "") && this.ConnRPipeAndHPipe(hyPipe.Piping, pipe);
			}
			this.ConnMSG = "选择的立管位置不合适，无法与消火栓侧边连接。";
			return result;
		}

		private bool RiserPipeConnSideDrant(Connector sideConn, YJKPiping<Pipe> hyPipe)
		{
			bool flag = false;
			XYZ xyz = sideConn.CoordinateSystem.BasisZ.Normalize();
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(sideConn.Origin, sideConn.Origin.Add(sideConn.CoordinateSystem.BasisZ));
			hyline2.MakeUnBound();
			XYZ project = hyline.GetProject(sideConn.Origin);
			XYZ project2 = hyline2.GetProject(project);
			XYZ xyz2 = project2.Subtract(sideConn.Origin).Normalize();
			double num = project2.DistanceTo(sideConn.Origin);
			double num2 = project.DistanceTo(project2);
			Pipe pipe = null;
			Pipe pipe2 = null;
			if (xyz2.IsAlmostEqualTo(xyz * -1.0, 0.0001))
			{
				this.ConnMSG = "选择的立管位置不合适，无法与消火栓侧边连接。";
				return flag;
			}
			if (num > 0.21325459317585302)
			{
				pipe = YJKRevitTools.NewPipe(hyPipe.Piping, project2, sideConn.Origin);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
			}
			if (num2 > 0.0065616797900262466)
			{
				pipe2 = YJKRevitTools.NewPipe(hyPipe.Piping, project2, project);
				pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
			}
			if (pipe == null)
			{
				this.ConnMSG = "选择的立管位置不合适，无法与消火栓侧边连接。";
			}
			else
			{
				this.PipeConnectHydrant(sideConn, pipe);
				this.doc.Regenerate();
				if (pipe2 == null)
				{
					flag = this.ConnPipeByElbowOrTee(hyPipe.Piping, pipe);
				}
				else
				{
					flag = this.ConnPipeByElbowOrTee(pipe, pipe2);
					if (flag)
					{
						flag = this.ConnPipeByElbowOrTee(hyPipe.Piping, pipe2);
					}
				}
			}
			return flag;
		}

		private bool RiserPipeConnDownHydrant(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyCrossPipe, ConnFireHydrant.PositionRelation positionRelation)
		{
			bool result = false;
			switch (positionRelation)
			{
			case ConnFireHydrant.PositionRelation.Invalid:
				this.ConnMSG = "管道相对于消火栓位置不合适，不能进行连接！";
				break;
			case ConnFireHydrant.PositionRelation.Opposite:
				this.ConnMSG = "管道相对于消火栓位置不合适，不能进行连接！";
				break;
			case ConnFireHydrant.PositionRelation.NoParallAndVertical:
				this.ConnMSG = "管道相对于消火栓位置不平行且不垂直，不能进行连接！";
				break;
			case ConnFireHydrant.PositionRelation.HorizontalCross:
			case ConnFireHydrant.PositionRelation.VerticalCross:
				result = this.CreatCrossPipeConn(hyPipe, hyCrossPipe);
				break;
			case ConnFireHydrant.PositionRelation.HorizontalStraight:
				result = this.ConnPipeByElbowOrTee(hyPipe.Piping, hyCrossPipe.Piping);
				break;
			case ConnFireHydrant.PositionRelation.LittleVerticalDistance:
			case ConnFireHydrant.PositionRelation.LittleHorizontalDistance:
				this.ConnMSG = "管道与消火栓位置间距不满足连接要求，不能进行连接！";
				break;
			case ConnFireHydrant.PositionRelation.VerticalHorizontalCross:
				result = this.CreatCrossPipesConn(hyPipe, hyCrossPipe);
				break;
			case ConnFireHydrant.PositionRelation.CrossConn:
				result = this.CreatCrossConnPipeConn(hyPipe, hyCrossPipe);
				break;
			case ConnFireHydrant.PositionRelation.HaveSlope:
				this.ConnMSG = "消火栓连接不支持带坡度管道！";
				break;
			}
			return result;
		}

		private bool CreatCrossPipeConn(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyCrossPipe)
		{
			Connector connector = hyCrossPipe.StartConnector.Connector.IsConnected ? hyCrossPipe.EndConnector.Connector : hyCrossPipe.StartConnector.Connector;
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(connector.Origin);
			Pipe pipe = YJKRevitTools.NewPipe(hyPipe.Piping, connector.Origin, project);
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
			this.doc.Regenerate();
			bool flag = this.ConnPipeByElbowOrTee(pipe, hyCrossPipe.Piping);
			if (flag)
			{
				flag = this.ConnPipeByElbowOrTee(hyPipe.Piping, pipe);
			}
			return flag;
		}

		private bool CreatCrossConnPipeConn(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyCrossPipe)
		{
			Connector connector = hyCrossPipe.StartConnector.Connector.IsConnected ? hyCrossPipe.EndConnector.Connector : hyCrossPipe.StartConnector.Connector;
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(connector.Origin);
			YJKLine hyline2 = new YJKLine(hyCrossPipe.StartConnector.Origin, hyCrossPipe.EndConnector.Origin);
			hyline2.MakeUnBound();
			XYZ project2 = hyline2.GetProject(project);
			Pipe pipe = YJKRevitTools.NewPipe(hyPipe.Piping, project2, project);
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
			this.doc.Regenerate();
			bool flag = this.ConnPipeByElbowOrTee(pipe, hyCrossPipe.Piping);
			if (flag)
			{
				flag = this.ConnPipeByElbowOrTee(hyPipe.Piping, pipe);
			}
			return flag;
		}

		private bool CreatCrossPipesConn(YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyCrossPipe)
		{
			Connector connector = hyCrossPipe.StartConnector.Connector.IsConnected ? hyCrossPipe.EndConnector.Connector : hyCrossPipe.StartConnector.Connector;
			XYZ origin = connector.Origin;
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(connector.Origin);
			XYZ endXYZ = new XYZ(origin.X, origin.Y, project.Z);
			Pipe pipe = YJKRevitTools.NewPipe(hyPipe.Piping, connector.Origin, endXYZ);
			Pipe pipe2 = YJKRevitTools.NewPipe(hyPipe.Piping, project, endXYZ);
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
			pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
			this.doc.Regenerate();
			bool flag = this.ConnPipeByElbowOrTee(pipe, hyCrossPipe.Piping);
			if (flag)
			{
				flag = this.ConnPipeByElbowOrTee(pipe, pipe2);
				if (flag)
				{
					flag = this.ConnPipeByElbowOrTee(hyPipe.Piping, pipe2);
				}
			}
			return flag;
		}

		private bool HorizontalPipeConnSideDrant(Connector sideConn, YJKPiping<Pipe> hyPipe)
		{
			bool result = false;
			if (Math.Abs(hyPipe.StartConnector.Origin.Z - hyPipe.EndConnector.Origin.Z) > 0.0001)
			{
				this.ConnMSG = "消火栓不支持带坡度管道连接。";
				return result;
			}
			new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin).MakeUnBound();
			XYZ vector = hyPipe.StartConnector.Origin.Subtract(hyPipe.EndConnector.Origin).Normalize();
			XYZ basisZ = sideConn.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(vector, basisZ))
			{
				this.ConnMSG = "管道相对于消火栓位置不平行且不垂直，不能进行连接！";
				return result;
			}
			Pipe extractionPipe = this.ExtractionPipe(sideConn, this.ExtendPipeLength);
			return this.ConnExtractionPipeAndMainPipe(hyPipe.Piping, extractionPipe);
		}

		private bool ConnExtractionPipeAndRiserPipe(Pipe rPipe, Pipe extractionPipe, bool flag)
		{
			bool flag2 = false;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(rPipe);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(extractionPipe);
			Connector pipeUnConnector = this.GetPipeUnConnector(hypiping2);
			XYZ projectPoint = hypiping.GetProjectPoint(pipeUnConnector.Origin, true);
			XYZ projectPoint2 = hypiping2.GetProjectPoint(projectPoint, true);
			XYZ basisZ = pipeUnConnector.CoordinateSystem.BasisZ;
			XYZ xyz = projectPoint2.Subtract(pipeUnConnector.Origin).Normalize();
			projectPoint2.DistanceTo(pipeUnConnector.Origin);
			double num = projectPoint.DistanceTo(projectPoint2);
			Pipe pipe = null;
			if (xyz.IsAlmostEqualTo(basisZ * -1.0, 0.0001))
			{
				if (flag)
				{
					this.ConnMSG = "选择的立管位置不合适，无法与消火栓底边连接。";
				}
				else
				{
					this.ConnMSG = "选择的立管位置不合适，无法与消火栓后边连接。";
				}
				return flag2;
			}
			if (num > 0.0065616797900262466)
			{
				pipe = YJKRevitTools.NewPipe(this.hyPipe.Piping, projectPoint2, projectPoint);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
			}
			if (extractionPipe == null)
			{
				if (flag)
				{
					this.ConnMSG = "选择的立管位置不合适，无法与消火栓底边连接。";
				}
				else
				{
					this.ConnMSG = "选择的立管位置不合适，无法与消火栓后边连接。";
				}
			}
			else if (pipe == null)
			{
				flag2 = this.ConnPipeByElbowOrTee(this.hyPipe.Piping, extractionPipe);
			}
			else
			{
				flag2 = this.ConnPipeByElbowOrTee(extractionPipe, pipe);
				if (flag2)
				{
					flag2 = this.ConnPipeByElbowOrTee(this.hyPipe.Piping, pipe);
				}
			}
			return flag2;
		}

		private bool ConnExtractionPipeAndMainPipe(Pipe hPipe, Pipe extractionPipe)
		{
			bool flag = false;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(hPipe);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(extractionPipe);
			Connector pipeUnConnector = this.GetPipeUnConnector(hypiping2);
			XYZ projectPoint = hypiping.GetProjectPoint(pipeUnConnector.Origin, true);
			XYZ xyz = new XYZ(pipeUnConnector.Origin.X, pipeUnConnector.Origin.Y, projectPoint.Z);
			double num = xyz.DistanceTo(projectPoint);
			double num2 = xyz.DistanceTo(pipeUnConnector.Origin);
			Pipe pipe = null;
			Pipe pipe2 = null;
			XYZ xyz2 = xyz.Subtract(projectPoint).Normalize();
			XYZ basisZ = pipeUnConnector.CoordinateSystem.BasisZ;
			if (hypiping.IsParallel(extractionPipe) && !hypiping.IsPipingOnLine(extractionPipe))
			{
				if (num2 > 0.0065616797900262466)
				{
					pipe = YJKRevitTools.NewPipe(hPipe, pipeUnConnector.Origin, xyz);
					pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
					this.doc.Regenerate();
				}
				pipe2 = YJKRevitTools.NewPipe(hPipe, projectPoint, xyz);
				pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
				if (pipe == null)
				{
					flag = this.ConnPipeByElbowOrTee(extractionPipe, pipe2);
					if (flag)
					{
						flag = this.ConnPipeByElbowOrTee(hPipe, pipe2);
					}
				}
				else
				{
					flag = this.ConnPipeByElbowOrTee(extractionPipe, pipe);
					if (flag)
					{
						flag = this.ConnPipeByElbowOrTee(pipe2, pipe);
						if (flag)
						{
							flag = this.ConnPipeByElbowOrTee(hPipe, pipe2);
						}
					}
				}
			}
			else if (hypiping.IsVertical(extractionPipe) && xyz2.IsAlmostEqualTo(basisZ * -1.0, 0.0001))
			{
				if (num2 > 0.0065616797900262466)
				{
					pipe = YJKRevitTools.NewPipe(hPipe, pipeUnConnector.Origin, xyz);
					pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
					this.doc.Regenerate();
					if (num > 0.0065616797900262466)
					{
						pipe2 = YJKRevitTools.NewPipe(hPipe, projectPoint, xyz);
						pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
						this.doc.Regenerate();
					}
				}
				if (pipe == null && pipe2 == null)
				{
					flag = this.ConnPipeByElbowOrTee(hPipe, extractionPipe);
				}
				else if (pipe2 == null)
				{
					flag = this.ConnPipeByElbowOrTee(extractionPipe, pipe);
					if (flag)
					{
						flag = this.ConnPipeByElbowOrTee(hPipe, pipe);
					}
				}
				else
				{
					flag = this.ConnPipeByElbowOrTee(extractionPipe, pipe);
					if (flag)
					{
						flag = this.ConnPipeByElbowOrTee(pipe2, pipe);
						if (flag)
						{
							flag = this.ConnPipeByElbowOrTee(hPipe, pipe2);
						}
					}
				}
			}
			return flag;
		}

		private bool HorizontalPipeConnSideDrant(YJKPiping<Pipe> hyPipe, Connector sideConn)
		{
			bool result = false;
			if (Math.Abs(hyPipe.StartConnector.Origin.Z - hyPipe.EndConnector.Origin.Z) > 0.0001)
			{
				this.ConnMSG = "消火栓不支持带坡度管道连接。";
				return result;
			}
			new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin).MakeUnBound();
			XYZ vector = hyPipe.StartConnector.Origin.Subtract(hyPipe.EndConnector.Origin).Normalize();
			XYZ basisZ = sideConn.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(vector, basisZ))
			{
				this.ConnMSG = "管道相对于消火栓位置不平行且不垂直，不能进行连接！";
				return result;
			}
			Pipe pipe = this.CreatSideConnExtractionPipe(sideConn, hyPipe);
			if (pipe == null)
			{
				result = false;
				this.ConnMSG = "管道距离消火栓位置不合适，不能进行连接！";
			}
			else
			{
				YJKPiping<Pipe> hyCrossPipe = new YJKPiping<Pipe>(pipe);
				Connector pipeUnConnector = this.GetPipeUnConnector(hyCrossPipe);
				ConnFireHydrant.PositionRelation positionRelation = this.CheckPositionRelations(this.GetCheckDeviation(), hyPipe.Piping, pipeUnConnector);
				result = this.RiserPipeConnDownHydrant(hyPipe, hyCrossPipe, positionRelation);
			}
			return result;
		}

		private bool ConnRPipeAndHPipe(Pipe riserPipe, Pipe horizontalPipe)
		{
			bool result = true;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(horizontalPipe);
			Connector connector = hypiping.StartConnector.Connector;
			Connector connector2 = hypiping.EndConnector.Connector;
			YJKLine hyline = new YJKLine(connector.Origin, connector2.Origin);
			Connector connector3 = connector.IsConnected ? connector2 : connector;
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(riserPipe);
			Connector connector4 = hypiping2.StartConnector.Connector;
			Connector connector5 = hypiping2.EndConnector.Connector;
			YJKLine hyline2 = new YJKLine(connector4.Origin, connector5.Origin);
			hyline2.MakeUnBound();
			XYZ project = hyline2.GetProject(connector3.Origin);
			hyline.MakeUnBound();
			XYZ project2 = hyline.GetProject(project);
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			try
			{
				if (!project2.Subtract(connector3.Origin).Normalize().IsAlmostEqualTo(connector3.CoordinateSystem.BasisZ, 0.0001))
				{
					this.ConnMSG = "立管距离消火栓位置不合适，不能进行连接！";
					return false;
				}
				if (project2.DistanceTo(connector3.Origin) < 0.31988188976377951)
				{
					this.ConnMSG = "立管距离消火栓位置不合适，不能进行连接！";
					return false;
				}
				if (project2.IsAlmostEqualTo(project))
				{
					hypipeConnect.NewSomeConnect(new List<Pipe>
					{
						riserPipe,
						horizontalPipe
					});
				}
				else
				{
					if (project2.DistanceTo(project) < 0.21325459317585302 + hypiping2.GetDiameter())
					{
						this.ConnMSG = "立管距离消火栓位置不合适，不能进行连接！";
						return false;
					}
					Pipe pipe = YJKRevitTools.NewPipe(riserPipe, project, project2);
					pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
					this.doc.Regenerate();
					hypipeConnect.NewSomeConnect(new List<Pipe>
					{
						pipe,
						horizontalPipe
					});
					hypipeConnect.NewSomeConnect(new List<Pipe>
					{
						riserPipe,
						pipe
					});
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ConnMSG = "连接失败！";
				result = false;
			}
			return result;
		}

		private void PipeConnectHydrant(Connector conn, Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Connector connector = hypiping.StartConnector.Connector;
			Connector connector2 = hypiping.EndConnector.Connector;
			if (connector2.Origin.IsAlmostEqualTo(conn.Origin, 0.0001))
			{
				connector2.ConnectTo(conn);
			}
			if (connector.Origin.IsAlmostEqualTo(conn.Origin, 0.0001))
			{
				connector.ConnectTo(conn);
			}
		}

		private void PipeConnectHydrant(FamilyInstance fiHydrant, Pipe pipe)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			IList<Connector> pipeConnectorEnd = connectHelper.GetPipeConnectorEnd(pipe);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fiHydrant, (Autodesk.Revit.DB.Domain)3);
			bool flag = false;
			foreach (Connector connector in pipeConnectorEnd)
			{
				foreach (Connector connector2 in hyelementConnector)
				{
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
		}

		private FamilyInstance MirrorDoubleHydrant(FamilyInstance familyInstance)
		{
			List<Connector> listConn = new List<Connector>();
			List<Connector> listConn2 = new List<Connector>();
			this.GetSideAndDownOrBackConns(familyInstance, ref listConn, ref listConn2);
			if (this.CheckDrantConned(listConn) || this.CheckDrantConned(listConn2))
			{
				return familyInstance;
			}
			familyInstance = this.MirrorUpDownDrant(familyInstance);
			return familyInstance;
		}

		private Pipe CreatSideConnExtractionPipe(Connector sideConn, YJKPiping<Pipe> hyPipe)
		{
			Pipe result = null;
			XYZ origin = sideConn.Origin;
			XYZ basisZ = sideConn.CoordinateSystem.BasisZ;
			YJKLine hyline = new YJKLine(hyPipe.StartConnector.Origin, hyPipe.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(origin);
			XYZ xyz = new XYZ(project.X, project.Y, origin.Z);
			double num = origin.DistanceTo(xyz);
			double num2 = project.DistanceTo(xyz);
			double num3 = (hyPipe.GetDiameter() + 0.21325459317585302) * 1.5;
			if (num2 < 0.0065616797900262466)
			{
				if (basisZ.IsParallelTo(hyline.LineVector))
				{
					if (num >= num3)
					{
						XYZ vector = xyz.Subtract(origin).Normalize();
						result = this.ExtractionPipe(sideConn, 0.31988188976377951, vector);
					}
				}
				else if (basisZ.IsPerpendicularTo(hyline.LineVector) && num >= num3)
				{
					result = this.ExtractionPipe(sideConn, 0.21325459317585302);
				}
			}
			else if (num < num3)
			{
				result = null;
			}
			else if (num >= num3)
			{
				XYZ vector2 = project.Subtract(xyz).Normalize();
				result = this.ExtractionPipe(sideConn, 0.31988188976377951, vector2);
			}
			return result;
		}

		private Pipe ExtractionPipe(Connector sideConn, double pipeLength)
		{
			Pipe pipe = null;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				XYZ origin = sideConn.Origin;
				XYZ endXYZ = origin.Add(sideConn.CoordinateSystem.BasisZ * pipeLength);
				pipe = YJKRevitTools.NewPipe(this.mainPipe, origin, endXYZ);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
				new YJKPiping<Pipe>(pipe).GetNearConnectorToPoint(origin).ConnectTo(sideConn);
				this.doc.Regenerate();
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				pipe = null;
				if (subTransaction != null)
				{
					subTransaction.RollBack();
				}
			}
			return pipe;
		}

		private Pipe ExtractionPipe(Connector downConn, Connector sideConn, double pipeLength)
		{
			Pipe result = null;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				XYZ origin = downConn.Origin;
				XYZ xyz = origin.Add(downConn.CoordinateSystem.BasisZ * 0.21325459317585302 * 2.0);
				Pipe pipe = YJKRevitTools.NewPipe(this.mainPipe, origin, xyz);
				XYZ endXYZ = xyz.Add(sideConn.CoordinateSystem.BasisZ * pipeLength);
				Pipe pipe2 = YJKRevitTools.NewPipe(this.mainPipe, xyz, endXYZ);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
				if (!this.ConnPipeByElbowOrTee(pipe, pipe2))
				{
					return result;
				}
				this.doc.Regenerate();
				Connector elbowUnConnConnector = this.GetElbowUnConnConnector(pipe);
				this.doc.DeleteElement(pipe);
				elbowUnConnConnector.ConnectTo(downConn);
				this.doc.Regenerate();
				new YJKRotateFamilyInstance().MovePiping(pipe2, downConn.CoordinateSystem.BasisZ);
				result = pipe2;
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
				if (subTransaction != null)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private Pipe ExtractionPipe(Connector sideConn, double pipeLength, XYZ vector)
		{
			Pipe result = null;
			if (!sideConn.CoordinateSystem.BasisZ.IsPerpendicularTo(vector))
			{
				return result;
			}
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				XYZ origin = sideConn.Origin;
				XYZ xyz = origin.Add(sideConn.CoordinateSystem.BasisZ * pipeLength);
				Pipe pipe = YJKRevitTools.NewPipe(this.mainPipe, origin, xyz);
				XYZ endXYZ = xyz.Add(vector * pipeLength);
				Pipe pipe2 = YJKRevitTools.NewPipe(this.mainPipe, xyz, endXYZ);
				pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(0.21325459317585302);
				this.doc.Regenerate();
				if (!this.ConnPipeByElbowOrTee(pipe, pipe2))
				{
					return result;
				}
				this.doc.Regenerate();
				Connector elbowUnConnConnector = this.GetElbowUnConnConnector(pipe);
				this.doc.DeleteElement(pipe);
				elbowUnConnConnector.ConnectTo(sideConn);
				this.doc.Regenerate();
				new YJKRotateFamilyInstance().MovePiping(pipe2, sideConn.CoordinateSystem.BasisZ);
				result = pipe2;
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
				if (subTransaction != null)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private Connector GetElbowUnConnConnector(Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			return YJKRevitTools.GetConnectConnector(hypiping.StartConnector.Connector.IsConnected ? hypiping.StartConnector.Connector : hypiping.EndConnector.Connector);
		}

		private bool ConnPipeByElbowOrTee(Pipe pipe1, Pipe pipe2)
		{
			IList<Pipe> list = new List<Pipe>();
			list.Add(pipe1);
			list.Add(pipe2);
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			bool result = false;
			try
			{
				result = hypipeConnect.NewSomeConnect(list);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
				this.ConnMSG = "位置不合适，不能进行连接！";
			}
			return result;
		}

		private FamilyInstance MirroSingleHydrant(FamilyInstance familyInstance, IList<Pipe> connPipes)
		{
			Connector connector = null;
			Connector connector2 = null;
			Connector connector3 = null;
			this.GetSideAndDownOrBackConns(familyInstance, ref connector, ref connector2, ref connector3);
			if ((connector != null && connector.IsConnected) || (connector2 != null && connector2.IsConnected) || (connector3 != null && connector3.IsConnected))
			{
				return familyInstance;
			}
			familyInstance = this.MirrorUpDownDrant(familyInstance);
			if (connector == null)
			{
				return familyInstance;
			}
			Pipe pipe = connPipes[0];
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			if (this.BeProjectOnHydrant(pipe, familyInstance, connector))
			{
				return familyInstance;
			}
			XYZ xyz = connector.CoordinateSystem.BasisZ.Normalize();
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ xyz2 = hyline.GetProject(connector.Origin);
			YJKLine hyline2 = new YJKLine(connector.Origin, connector.Origin.Add(xyz));
			hyline2.MakeUnBound();
			XYZ xyz3 = hyline2.GetProject(xyz2).Subtract(connector.Origin).Normalize();
			if (xyz.IsAlmostEqualTo(xyz3, 0.0001) || xyz3.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
			{
				return familyInstance;
			}
			xyz2 = new XYZ(xyz2.X, xyz2.Y, connector.Origin.Z);
			XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, 0.5);
			XYZ xyz4 = (xyz2 - connectorDirectionPoint).Normalize();
			if (xyz.AngleTo(xyz4) / Math.PI * 180.0 > 90.0)
			{
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(familyInstance.HandOrientation, ((LocationPoint)familyInstance.Location).Point);
				List<ElementId> list = new List<ElementId>();
				list.Add(familyInstance.Id);
				list = RevitVersionFuncs.MirrorElements(familyInstance.Document, list, plane, BuiltInCategory.OST_MechanicalEquipment, false);
				if (list != null && list.Count > 0)
				{
					familyInstance = (this.doc.GetElement(list.FirstOrDefault<ElementId>()) as FamilyInstance);
				}
			}
			return familyInstance;
		}

		private bool BeProjectOnHydrant(Pipe pipe, FamilyInstance fiHydrant, Connector sideConn)
		{
			bool result = false;
			if (!YJKRevitTools.isRiserPipe(pipe) || sideConn == null)
			{
				return result;
			}
			YJKLine hyline = new YJKLine(sideConn.Origin, sideConn.Origin.Add(sideConn.CoordinateSystem.BasisZ));
			hyline.MakeUnBound();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ project = hyline.GetProject(hypiping.StartConnector.Origin);
			BoundingBoxXYZ boundingBoxXYZ = fiHydrant.get_BoundingBox(fiHydrant.Document.ActiveView);
			Transform transform = fiHydrant.GetTransform();
			Outline outline;
			if (transform != null && transform.IsTranslation)
			{
				outline = new Outline(transform.OfPoint(boundingBoxXYZ.Min), transform.OfPoint(boundingBoxXYZ.Max));
			}
			else
			{
				outline = new Outline(boundingBoxXYZ.Min, boundingBoxXYZ.Max);
			}
			if (outline.Contains(project, 0.0001))
			{
				result = true;
			}
			return result;
		}

		private FamilyInstance MirroHydrant(FamilyInstance familyInstance, IList<Pipe> connPipes)
		{
			if (this.beDouble)
			{
				familyInstance = this.MirrorDoubleHydrant(familyInstance);
			}
			else
			{
				familyInstance = this.MirroSingleHydrant(familyInstance, connPipes);
			}
			return familyInstance;
		}

		private FamilyInstance MirrorUpDownDrant(FamilyInstance familyInstance)
		{
			Transform transform = familyInstance.GetTransform();
			Element element = familyInstance;
			Document document = familyInstance.Document;
			BoundingBoxXYZ boundingBoxXYZ = element.get_BoundingBox(familyInstance.Document.ActiveView);
			Outline outline;
			if (transform != null && transform.IsTranslation)
			{
				outline = new Outline(transform.OfPoint(boundingBoxXYZ.Min), transform.OfPoint(boundingBoxXYZ.Max));
			}
			else
			{
				outline = new Outline(boundingBoxXYZ.Min, boundingBoxXYZ.Max);
			}
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(outline);
			List<Element> list = new FilteredElementCollector(familyInstance.Document).OfClass(typeof(Wall)).WherePasses(boundingBoxIntersectsFilter).ToElements().ToList<Element>();
			foreach (LinkedDocument linkedDocument in familyInstance.Document.GetLinkedDocuments())
			{
				list.AddRange(new FilteredElementCollector(linkedDocument.Document).OfClass(typeof(Wall)).WherePasses(boundingBoxIntersectsFilter).ToElements());
			}
			if (list != null && list.Count > 0)
			{
				GeometryElement geometryElement = this.GetParralWallToFamilyInstance(list, familyInstance).get_Geometry(new Options
				{
                    DetailLevel = (ViewDetailLevel)3
				});
				bool flag = true;
				foreach (GeometryObject geometryObject in geometryElement)
				{
					if (geometryObject is Solid)
					{
						foreach (object obj in ((Solid)geometryObject).Faces)
						{
							if (obj is PlanarFace)
							{
								IntersectionResult intersectionResult = ((PlanarFace)obj).Project(((LocationPoint)familyInstance.Location).Point);
								if (intersectionResult != null && intersectionResult.Distance < Math.Abs(0.001))
								{
									flag = false;
									break;
								}
							}
						}
					}
				}
				if (this.BeInWall(geometryElement, familyInstance))
				{
					flag = false;
				}
				if (flag)
				{
					XYZ xyz = null;
					foreach (object obj2 in ((Solid)geometryElement.FirstOrDefault<GeometryObject>()).Faces)
					{
						if (obj2 is PlanarFace && ((PlanarFace)obj2).FaceNormal().IsAlmostEqualTo(familyInstance.FacingOrientation) && ((PlanarFace)obj2).Project(((LocationPoint)familyInstance.Location).Point) != null)
						{
							xyz = ((PlanarFace)obj2).Project(((LocationPoint)familyInstance.Location).Point).XYZPoint;
						}
					}
					if (xyz != null)
					{
						Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(familyInstance.FacingOrientation, (xyz + ((LocationPoint)familyInstance.Location).Point) / 2.0);
						List<ElementId> list2 = new List<ElementId>();
						list2.Add(familyInstance.Id);
						list2 = RevitVersionFuncs.MirrorElements(familyInstance.Document, list2, plane, BuiltInCategory.OST_MechanicalEquipment, false);
						if (list2 != null && list2.Count > 0)
						{
							familyInstance = (document.GetElement(list2.FirstOrDefault<ElementId>()) as FamilyInstance);
						}
					}
				}
			}
			return familyInstance;
		}

		private Wall GetParralWallToFamilyInstance(List<Element> listElem, FamilyInstance fi)
		{
			Wall result = null;
			if (listElem.Count == 1)
			{
				result = (listElem.First<Element>() as Wall);
			}
			else if (listElem.Count > 1)
			{
				XYZ facingOrientation = fi.FacingOrientation;
				foreach (Element element in listElem)
				{
					Wall wall = element as Wall;
					XYZ orientation = wall.Orientation;
					if (facingOrientation.IsAlmostEqualTo(orientation, 0.0001) || facingOrientation.IsAlmostEqualTo(orientation * -1.0, 0.0001))
					{
						result = wall;
						break;
					}
				}
			}
			return result;
		}

		private bool BeInWall(GeometryElement geoElem, FamilyInstance fi)
		{
			bool result = false;
			XYZ facingOrientation = fi.FacingOrientation;
			PlanarFace planarFace = null;
			PlanarFace planarFace2 = null;
			foreach (object obj in ((Solid)geoElem.FirstOrDefault<GeometryObject>()).Faces)
			{
				if (obj is PlanarFace)
				{
					XYZ xyz = ((PlanarFace)obj).FaceNormal();
					if (xyz.IsAlmostEqualTo(facingOrientation, 0.0001))
					{
						planarFace = (PlanarFace)obj;
					}
					if (xyz.IsAlmostEqualTo(facingOrientation * -1.0, 0.0001))
					{
						planarFace2 = (PlanarFace)obj;
					}
				}
			}
			if (planarFace == null || planarFace2 == null)
			{
				return result;
			}
			LocationPoint locationPoint = fi.Location as LocationPoint;
			if (locationPoint == null)
			{
				return result;
			}
			try
			{
				XYZ point = locationPoint.Point;
				XYZ xyzpoint = planarFace.Project(point).XYZPoint;
				XYZ xyzpoint2 = planarFace2.Project(point).XYZPoint;
				XYZ xyz2 = point.Subtract(xyzpoint).Normalize();
				XYZ xyz3 = point.Subtract(xyzpoint2).Normalize();
				if (xyz2.IsAlmostEqualTo(facingOrientation, 0.0001) || xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
				{
					result = false;
				}
				else if (xyz3.IsAlmostEqualTo(facingOrientation, 0.0001) || xyz3.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
				{
					result = true;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = true;
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector, double distance)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + distance * coordinateSystem.BasisZ;
			}
			return result;
		}

		private void GetSideAndDownOrBackConns(FamilyInstance familyInstance, ref List<Connector> sideConns, ref List<Connector> backConns)
		{
			sideConns = new List<Connector>();
			backConns = new List<Connector>();
			XYZ handOrientation = familyInstance.HandOrientation;
			XYZ facingOrientation = familyInstance.FacingOrientation;
			IEnumerable<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(familyInstance);
			double num = 0.0001;
			foreach (Connector connector in elementAllConnector)
			{
				if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					XYZ basisZ = connector.CoordinateSystem.BasisZ;
					if (basisZ.IsAlmostEqualTo(handOrientation, num) || basisZ.IsAlmostEqualTo(handOrientation * -1.0, num))
					{
						sideConns.Add(connector);
					}
					else if (basisZ.IsAlmostEqualTo(facingOrientation, num))
					{
						backConns.Add(connector);
					}
				}
			}
		}

		private void GetSideAndDownOrBackConns(FamilyInstance familyInstance, ref Connector sideConn, ref Connector downConn, ref Connector backConn)
		{
			XYZ handOrientation = familyInstance.HandOrientation;
			XYZ xyz = new XYZ(0.0, 0.0, -1.0);
			XYZ facingOrientation = familyInstance.FacingOrientation;
			IEnumerable<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(familyInstance);
			double num = 0.0001;
			foreach (Connector connector in elementAllConnector)
			{
				if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					XYZ basisZ = connector.CoordinateSystem.BasisZ;
					if (basisZ.IsAlmostEqualTo(handOrientation, num) || basisZ.IsAlmostEqualTo(handOrientation * -1.0, num))
					{
						sideConn = connector;
					}
					else if (basisZ.IsAlmostEqualTo(xyz, num))
					{
						downConn = connector;
					}
					else if (basisZ.IsAlmostEqualTo(facingOrientation, num))
					{
						backConn = connector;
					}
				}
			}
		}

		private bool CheckDrantConned(IList<Connector> listConn)
		{
			bool result = false;
			if (listConn == null || listConn.Count < 1)
			{
				return true;
			}
			IEnumerator<Connector> enumerator = listConn.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.IsConnected)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private bool CheckSegmentFitting()
		{
			IList<ElementId> list = new List<ElementId>();
			PipeType pipeType = this.mainPipe.PipeType;
			list = UnifiedModified.GetSegmentFitting(pipeType, 1);
			if (list == null || list.Count < 1)
			{
				this.ConnMSG = "管道弯头连接首选项没有设置，不能进行连接！";
				return false;
			}
			list = UnifiedModified.GetSegmentFitting(pipeType, 2);
			if (list == null || list.Count < 1)
			{
				this.ConnMSG = "管道三通连接首选项没有设置，不能进行连接！";
				return false;
			}
			list = UnifiedModified.GetSegmentFitting(pipeType, 4);
			if (list == null || list.Count < 1)
			{
				this.ConnMSG = "管道变径连接首选项没有设置，不能进行连接！";
				return false;
			}
			return true;
		}

		private ConnFireHydrant.PositionRelation CheckPositionRelations(Pipe pipe, Connector connector, double deviation)
		{
			ConnFireHydrant.PositionRelation result = ConnFireHydrant.PositionRelation.Invalid;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ lineVector = hyline.LineVector;
			XYZ origin = connector.Origin;
			XYZ basisZ = connector.CoordinateSystem.BasisZ;
			if (!lineVector.IsParallelTo(basisZ) && !lineVector.IsPerpendicularTo(basisZ))
			{
				return ConnFireHydrant.PositionRelation.NoParallAndVertical;
			}
			XYZ project = hyline.GetProject(origin);
			XYZ xyz = new XYZ(project.X, project.Y, origin.Z);
			double num = origin.DistanceTo(xyz);
			double num2 = project.DistanceTo(xyz);
			double num3 = 0.0065616797900262466;
			XYZ xyz2 = xyz.Subtract(origin).Normalize();
			if (xyz2.IsParallelTo(basisZ) && !xyz2.IsAlmostEqualTo(basisZ, 0.0001))
			{
				result = ConnFireHydrant.PositionRelation.Opposite;
			}
			else if (num2 < num3)
			{
				if (num >= deviation)
				{
					if (lineVector.IsParallelTo(basisZ))
					{
						result = ConnFireHydrant.PositionRelation.HorizontalCross;
					}
					if (lineVector.IsPerpendicularTo(basisZ))
					{
						result = ConnFireHydrant.PositionRelation.HorizontalStraight;
					}
				}
			}
			else if (num2 < deviation)
			{
				result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
			}
			else if (num < num3)
			{
				result = ConnFireHydrant.PositionRelation.VerticalCross;
			}
			else if (num < deviation)
			{
				result = ConnFireHydrant.PositionRelation.LittleHorizontalDistance;
			}
			else
			{
				result = ConnFireHydrant.PositionRelation.VerticalHorizontalCross;
			}
			return result;
		}

		private ConnFireHydrant.PositionRelation CheckPositionRelations(double deviation, Pipe pipe, Connector connector)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			XYZ lineVector = hyline.LineVector;
			XYZ origin = connector.Origin;
			XYZ basisZ = connector.CoordinateSystem.BasisZ;
			if (!this.BeParallAndVertical(lineVector, basisZ))
			{
				return ConnFireHydrant.PositionRelation.NoParallAndVertical;
			}
			XYZ project = hyline.GetProject(origin);
			XYZ xyz = new XYZ(project.X, project.Y, origin.Z);
			YJKLine hyline2 = new YJKLine(origin, origin.Add(basisZ));
			hyline2.MakeUnBound();
			XYZ xyz2 = hyline2.GetProject(project).Subtract(origin).Normalize();
			if (xyz2.IsAlmostEqualTo(basisZ * -1.0, 0.0001) && xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
			{
				return ConnFireHydrant.PositionRelation.Opposite;
			}
			double num = origin.DistanceTo(xyz);
			double num2 = project.DistanceTo(xyz);
			double num3 = 0.0032808398950131233;
			ConnFireHydrant.PositionRelation result;
			if (YJKRevitTools.isRiserPipe(pipe))
			{
				xyz = hyline2.GetProject(project);
				double num4 = xyz.DistanceTo(origin);
				double num5 = xyz.DistanceTo(project);
				if (num5 < num3)
				{
					if (num4 >= deviation)
					{
						result = ConnFireHydrant.PositionRelation.HorizontalStraight;
					}
					else
					{
						result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
					}
				}
				else if (num5 < deviation)
				{
					result = ConnFireHydrant.PositionRelation.LittleHorizontalDistance;
				}
				else if (num4 < deviation)
				{
					result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
				}
				else
				{
					result = ConnFireHydrant.PositionRelation.CrossConn;
				}
			}
			else if (!this.BeHorizontalPipe(hypiping.Piping))
			{
				result = ConnFireHydrant.PositionRelation.HaveSlope;
			}
			else if (lineVector.IsParallelTo(basisZ))
			{
				if (num2 < num3)
				{
					if (num >= deviation)
					{
						result = ConnFireHydrant.PositionRelation.HorizontalCross;
					}
					else
					{
						result = ConnFireHydrant.PositionRelation.LittleHorizontalDistance;
					}
				}
				else if (num2 < deviation)
				{
					result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
				}
				else if (num < num3)
				{
					result = ConnFireHydrant.PositionRelation.VerticalCross;
				}
				else if (num >= deviation)
				{
					result = ConnFireHydrant.PositionRelation.VerticalHorizontalCross;
				}
				else
				{
					result = ConnFireHydrant.PositionRelation.LittleHorizontalDistance;
				}
			}
			else if (lineVector.IsPerpendicularTo(basisZ))
			{
				if (num2 < num3)
				{
					if (num >= deviation)
					{
						result = ConnFireHydrant.PositionRelation.HorizontalStraight;
					}
					else
					{
						result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
					}
				}
				else if (num2 < num3)
				{
					result = ConnFireHydrant.PositionRelation.LittleVerticalDistance;
				}
				else if (num < num3)
				{
					result = ConnFireHydrant.PositionRelation.VerticalCross;
				}
				else if (num >= deviation)
				{
					result = ConnFireHydrant.PositionRelation.VerticalHorizontalCross;
				}
				else
				{
					result = ConnFireHydrant.PositionRelation.LittleHorizontalDistance;
				}
			}
			else
			{
				result = ConnFireHydrant.PositionRelation.NoParallAndVertical;
			}
			return result;
		}

		private bool BeHorizontalPipe(Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			return Math.Abs(origin.Z - origin2.Z) <= 0.001;
		}

		private bool BeParallAndVertical(XYZ vector1, XYZ vector2)
		{
			YJKLine hyline = new YJKLine(vector1, vector1.Add(vector1));
			YJKLine hyline2 = new YJKLine(vector2, vector2.Add(vector2));
			return hyline.IsParallel(hyline2.Line) || hyline.IsVertical(hyline2.Line);
		}

		private FamilyInstance fiHyDrant;

		private IList<Pipe> listPipe;

		private Pipe mainPipe;

		private YJKPiping<Pipe> hyPipe;

		private Document doc;

		private bool beDouble;

		private const double DRANTCONNDIAMETER = 0.21325459317585302;

		private const double DEVVECTOR = 0.0001;

		private const double BESAMEPLANTDEV = 0.0065616797900262466;

		public double ExtendPipeLength = 0.65616797900262469;

		private double backConnPipeLength;

		private enum PositionRelation
		{
			Invalid,
			Opposite,
			NoParallAndVertical,
			HorizontalCross,
			HorizontalStraight,
			LittleVerticalDistance,
			VerticalCross,
			LittleHorizontalDistance,
			VerticalHorizontalCross,
			CrossConn,
			HaveSlope
		}
	}
}
